<script setup>
import { ref, onMounted, defineProps } from 'vue'
import Map from '@/utils/map'
import { townList, districtName } from '@/data/map'
import { useMapStore } from '@/stores/map'

const mapStore = useMapStore(),
  props = defineProps({
    config: {
      type: Object,
      default: () => {
        return {}
      },
    },
  })

const containerRef = ref()

class PeopleMap extends Map {
  constructor(...args) {
    super(...args)
  }

  // 区/县地图初始化前
  beforeInitDistrictMap() {
    this.label = new AMap.Marker({
      anchor: 'center',
      visible: false,
      offset: new AMap.Pixel(50, -50),
    })

    // this.label.on('click', e => {
    //   console.log('this.town,', this.town)
    // })

    this.map.add(this.label)
  }

  // 获取地图模块标签信息弹窗
  getPolygonLabelModal(name = '') {
    const modalType = props?.config?.modalType || ''
    let obj = []

    if (!modalType) return ''

    switch (modalType) {
      case 'community':
        obj = mapStore?.mapMonitorQuantity?.find(item => item.label === name) || {}

        return `<div class="people-marker">
            <div class="name">${name}</div>
            <div class="desc">监控点数量：${obj?.value || 0}个</div>
          </div>`
      case 'naTest':
        obj = mapStore?.mapNACollectionAmount?.find(item => item.label === name) || {}

        return `<div class="people-marker">
          <div class="name">${name}</div>
          <div class="desc-box">
            <div class="desc-model">
              <div class="desc">应采：</div>
              <div class="desc">${obj?.ycrs || 0}人</div>
            </div>
            <div class="desc-model">
              <div class="desc">已采：</div>
              <div class="desc">${obj?.total || 0}人</div>
            </div>
          </div>
          <div class="desc-box">
            <div class="desc-model">
              <div class="desc">未采</div>
              <div class="desc">${obj?.wcrs || 0}人</div>
            </div>
            <div class="desc-model">
              <div class="desc">检测率：</div>
              <div class="desc">${obj?.percen || 0}%</div>
            </div>
          </div>
        </div>`
      case 'people':
        obj = mapStore?.mapPermanentPopulation?.find(item => item.label === name) || {}

        return `<div class="people-marker">
          <div class="name">${name}</div>
          <div class="desc-box">
            <div class="desc-model">
              <div class="desc">常住人口：</div>
              <div class="desc">${obj?.cz || 0}人</div>
            </div>
            <div class="desc-model">
              <div class="desc">外出人口：</div>
              <div class="desc">${obj?.fcz || 0}人</div>
            </div>
          </div>
        </div>`
      default:
        return ''
    }
  }

  // 区/县边界创建后
  afterCreatePolygon(polygon, gjson) {
    const name = gjson.properties.name

    // 点击绘制的区域时执行其他交互
    polygon.on('click', e => {})

    if (name == districtName) return

    // 鼠标经过
    polygon.on('mouseover', e => {
      const townItem = townList[name]
      if (!townItem) return

      this.town = name

      polygon.setOptions({
        fillColor: this.config.bgHover,
      })

      const html = this.getPolygonLabelModal(name) || ''

      if (html) {
        this.label.setContent(html)
        this.label.setPosition(townItem.center)
        this.label.show()
      }
    })

    // 鼠标移除
    polygon.on('mouseout', e => {
      polygon.setOptions({
        fillColor: this.config.bg,
      })
    })
  }
}

onMounted(() => {
  new PeopleMap(containerRef.value, {
    zoom: 10.3,
  })
})
</script>

<template>
  <div class="map">
    <div ref="containerRef" class="container"></div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  background: none !important;
}
</style>
<style lang="scss">
.people-marker {
  //   background-color: #fff;
  // background: rgba($color: #fff, $alpha: 0.9);
  min-width: 140px;
  border-radius: 4px;
  font-size: 12px;
  color: #fff;
  .name {
    font-size: 14px;
    background: #0071b7;
    padding: 2px 5px;
    text-align: center;
  }
  .desc-box {
    width: 250px;
    display: flex;
  }
  .desc-model {
    flex: 1;
  }
  .desc {
    background: #002434;
    padding: 3px 5px;
  }
}
</style>
